CreateEventAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 28
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 20 2
1
import {
2
  Controller,
3
  Inject,
4
  Post,
5
  Body,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import { AuthGuard } from '@nestjs/passport';
10
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
11
import { CreateEventCommand } from 'src/Application/Calendar/Command/CreateEventCommand';
12
import { ICommandBus } from 'src/Application/ICommandBus';
13
import { UserRole } from 'src/Domain/User/User.entity';
14
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
15
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
16
import { EventDTO } from '../DTO/EventDTO';
17
18
@Controller('events')
19
@ApiTags('Calendar')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class CreateEventAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.PHOTOGRAPHER)
30
  @ApiOperation({ summary: 'Create new event' })
31
  public async index(@Body() dto: EventDTO) {
32
    const { date, schoolId, userId, summary } = dto;
33
34
    try {
35
      const id = await this.commandBus.execute(
36
        new CreateEventCommand(
37
          new Date(date),
38
          userId,
39
          schoolId,
40
          summary
41
        )
42
      );
43
44
      return { id };
45
    } catch (e) {
46
      throw new BadRequestException(e.message);
47
    }
48
  }
49
}
50